//# sourceURL=http://uMapApp/Module/Bridge/BridgeSinglePillar
define([
    "mhjs/mhjs"
    , "uMap/uMap"
    , "uEarth/uEarth"
    , "../../UI/Layer"
    , "../../Earth/ViewPort"
    , "../StakeNum/StakeNum"
], function (
    mhjs,
    uMap,
    uEarth,
    Layer,
    ViewPort,
    StakeNum
) {

    function BridgeSinglePillar(option) {
        var _this = this;
        option && _this.OptionToProperties(option);

        if (_this.position) {

            _this.layer = new Layer({
                "dataSource": _this.dataSource,
                "app"       : _this.app,
                "viewport"  : new ViewPort({
                    "center": _this.position,
                    "hpr"   : _this.viewport.orientation
                })
            });
            _this.layer.dataSourceView.on("loaded", function () {

                _this.features = {};
                _this.layer.dataSourceView.tileSet.object.tileLoad.addEventListener(function (tile) {

                    if (tile.content.innerContents) {

                        $.each(tile.content.innerContents, function (i, innerContent) {

                            for (var j = 0; j < innerContent.featuresLength; j++) {
                                var feature = innerContent.getFeature(j);
                                if (feature && feature.getProperty) {
                                    var DbId = parseInt(feature.getProperty("DbId"));
                                    _this.features[DbId] = _this.features[DbId] || [];
                                    _this.features[DbId].push(feature);
                                }
                            }

                        });


                    } else if (tile.content) {

                        for (var i = 0; i < tile.content.featuresLength; i++) {
                            var feature = tile.content.getFeature(i);
                            if (feature && feature.getProperty) {
                                var DbId = parseInt(feature.getProperty("DbId"));
                                _this.features[DbId] = _this.features[DbId] || [];
                                _this.features[DbId].push(feature);
                            }
                        }
                    }

                });

                _this.layer.dataSourceView.tileSet.on("click", function (event) {

                    if (event.pickObject) {
                        var feature = event.pickObject;
                        console.log(feature.getProperty("DbId"));
                    }

                });


            });

            _this.styles = {
                normal   : ["#FFF", 1],
                highlight: ["#FF0", 1],
                active   : ["#F00", 1]
            };



            _this.marker = new uMap.Entity({
                objectOption: {
                    position : _this.position,
                    billboard: new uMap.BillboardGraphic({
                        objectOption: {
                            image         : _this.app.icons.getIconMarker(_this.app.icons.bridge),
                            verticalOrigin: "BOTTOM"
                            // , disableDepthTestDistance: Number.POSITIVE_INFINITY
                        }
                    }),
                    label    : new uMap.LabelGraphic({
                        objectOption: {
                            text                      : _this.title,
                            verticalOrigin            : "TOP"
                            , disableDepthTestDistance: Number.POSITIVE_INFINITY
                        }
                    })
                }
            });

            _this.marker.on("click", function () {
                _this.trigger("click", _this);
            });

            _this.layer.dataSources.entities.add(_this.marker.object);
        }


    }

    var _class = BridgeSinglePillar;

    mhjs.OptionToPropertiesInterface(_class);
    mhjs.EventInterface(_class);

    Object.defineProperties(_class.prototype, {
        "open"  : {
            value: function () {
                if (!this.isOpened) {
                    this.layer && this.layer.open();
                    this.isOpened = true;
                    this.trigger("open");
                }
            }
        },
        "close" : {
            value: function () {
                if (this.isOpened) {
                    this.layer && this.layer.close();
                    this.isOpened = false;
                    this.trigger("close");
                }
            }
        },
        "toggle": {
            value: function () {
                if (this.isOpened) {
                    this.close();
                } else {
                    this.open();
                }
            }
        },
        "active": {
            value: function (isActive) {
                var _this = this;

                isActive = (isActive === undefined) || Boolean(isActive);
                _this.isActive = _this.isActive === undefined ? false : _this.isActive;

                if (isActive !== _this.isActive) {

                    if (isActive) {

                        // if (_this.layer) {
                        //     _this.layer.active();
                        //     _this.layer.dataSourceView.open();
                        // }

                        if (_this.DbIds) {
                            $.each(_this.DbIds, function (i, DbId) {
                                if (_this.features[DbId]) {
                                    $.each(_this.features[DbId], function (j, feature) {
                                        feature.color = new uMap.Color(_this.styles.active);
                                    });
                                }
                            });
                        }

                        _this.marker.label.fillColor = new uMap.Color(["#ff0"]);
                        _this.marker.label.scale = 1.2;
                        _this.marker.billboard.image = _this.app.icons.getIconMarker(_this.app.icons.bridge, true);

                        _this.trigger("active");

                    } else {

                        // if (_this.layer) {
                        //     _this.layer.dataSourceView.close();
                        // }

                        if (_this.DbIds) {
                            $.each(_this.DbIds, function (i, DbId) {
                                if (_this.features[DbId]) {
                                    $.each(_this.features[DbId], function (j, feature) {
                                        feature.color = new uMap.Color(_this.styles.normal);
                                    });
                                }
                            });
                        }

                        _this.marker.label.fillColor = new uMap.Color(["#FFF"]);
                        _this.marker.label.scale = 1;
                        _this.marker.billboard.image = _this.app.icons.getIconMarker(_this.app.icons.bridge);

                        _this.trigger("unActive");
                    }

                    _this.isActive = isActive;

                }


            }
        }
    });

    return _class;

});